package com.example.sunsg.okhttptest.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.breadtrip.R;
import com.example.sunsg.okhttptest.TheApplication;
import com.example.sunsg.okhttptest.adapter.RecyclerViewAdapter;
import com.example.sunsg.okhttptest.bean.TestAbstract;
import com.example.sunsg.okhttptest.bean.TestInterface;
import com.example.sunsg.okhttptest.util.Util;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewActivity extends AppCompatActivity {
//    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRecycler;
    private RecyclerViewAdapter mAdapter;
    public final static int SpanCount = 4;
    private Handler mHandler = new Handler();

    public static void launch(Context context){
        context.startActivity(new Intent(context, RecyclerViewActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        initView();
        addListener();
        initData();
        TestAbstract testAbstract = new TestInterface();

        int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
        Util.quicksort(a,0,a.length - 1);
        for(int i=0;i<a.length;i++)
            System.out.println(a[i]);

    }

    private void initView(){
//        mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh);
        mRecycler = (RecyclerView) findViewById(R.id.id_stickynavlayout_bottom);
        setLayoutManager();
        mAdapter = new RecyclerViewAdapter(getApplicationContext(),LinearLayoutManager.VERTICAL);
        mRecycler.setAdapter(mAdapter);

    }

    private void addListener(){
//        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                mHandler.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        mRefreshLayout.setRefreshing(false);
//                    }
//                }, 3000);
//            }
//        });
        mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
//                Log.i("test","dx = "+dx + " dy ="+dy);
            }
        });
    }

    private void setLayoutManager(){
//        LinearLayoutManager manager = new LinearLayoutManager(getApplicationContext());
        final GridLayoutManager manager= new GridLayoutManager(getApplicationContext(),SpanCount);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
//        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(SpanCount,StaggeredGridLayoutManager.HORIZONTAL);
        mRecycler.setLayoutManager(manager);
//        mRecycler.addItemDecoration(new RecyclerDividerItemDecoradion(getApplicationContext(),5));
        GridLayoutManager.SpanSizeLookup look = new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                RecyclerViewAdapter.Item item = (RecyclerViewAdapter.Item) mAdapter.getItem(position);
                if(item.type == RecyclerViewAdapter.Item.TYPE_1){
                    return 4;
                }else if(item.type == RecyclerViewAdapter.Item.TYPE_MORE){
                    return SpanCount;
                }else if(item.type == RecyclerViewAdapter.Item.TYPE_THREE){
                    return 3;
                }
                return SpanCount;
            }
        };
        manager.setSpanSizeLookup(look);
        mRecycler.addItemDecoration(new MyDecoration());
    }

    private void initData(){
        List<RecyclerViewAdapter.Item> items = new ArrayList<>();
        RecyclerViewAdapter.Item item = null;
        RecyclerViewAdapter.Item item2 = null;
        int position = 0;
        for (int i = 'A';i <= 'z';i++){
            item = new RecyclerViewAdapter.Item();
            item.name = ""+(char)i;
//            item.random = (float) (Math.random() +1.5);
            item.random = (float) 1.5;
            if((position) % 6 == 0){
                item.type = RecyclerViewAdapter.Item.TYPE_MORE;
                items.add(item);
            }else{
                item.type = RecyclerViewAdapter.Item.TYPE_1;

                for(int j = 0; j < 4 ;j++){
                    item2 = new RecyclerViewAdapter.Item();
                    item2.type = RecyclerViewAdapter.Item.TYPE_THREE;
                    item2.name = i + i * j +"";
                    if(j == 0){
                        item2.isLineFirst = true;
                    }else if(j == 3){
                        item2.isLineLast = true;
                    }
                    items.add(item2);
                }
                item2 = new RecyclerViewAdapter.Item();
                item2.type = RecyclerViewAdapter.Item.TYPE_FOUR;
                items.add(item2);

                items.add(item);
                items.add(item);
                items.add(item);

            }

            position++;
        }
        mAdapter.setItem(items);
    }

    private static class MyDecoration extends RecyclerView.ItemDecoration{
        private static final int line_height = 5;
        private int padding5;
        private int padding6;
        public MyDecoration() {
            padding5 = Util.dp2px(TheApplication.getApplication(),5);
            padding6 = Util.dp2px(TheApplication.getApplication(),6);
        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDraw(c, parent, state);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
//            super.getItemOffsets(outRect, view, parent, state);
            int poition = parent.getChildAdapterPosition(view);

            RecyclerViewAdapter myAdapter = (RecyclerViewAdapter) parent.getAdapter();
            int type = myAdapter.getItemViewType(poition);
            if(type == RecyclerViewAdapter.Item.TYPE_THREE){
                RecyclerViewAdapter.Item item = (RecyclerViewAdapter.Item) myAdapter.getItem(poition);
                if(item.isLineFirst){
                    //每一行的第一的
                    outRect.set(padding5, 0, 0, 0);
                }else if(item.isLineLast){
                    //每一行的最后一个
                    outRect.set(padding5, 0, padding5, 0);
                }else{
                    //每一行的其他
                    outRect.set(padding5, 0, 0, 0);
                }
            }

        }
    }



}
